var hdf = hdf || {};

hdf.init = function() {
	hdf.scrollTop();
	hdf.stickyQMenu();
	hdf.stickySidebar();
	hdf.fireRateBoard();
};

hdf.fireRateBoard = function() {
	var target = $('#hdf-fireratebd'),
		board = $('#hdf-frate');

	$(document).on('click scroll', function(e) {
		if ($(e.target).closest(board).length === 0) {
            if(board.is(':visible') && board.hasClass('hdf-frate-expand')) {
				board.removeClass('hdf-frate-expand').attr('style','');
			}
        }
	});

	target.on('click', function(e) {
		e.stopPropagation();
		var left = target.offset().left-1,
			offset_y = target.offset().top,
			size_h = parseInt(target.parent().parent().outerHeight()),
			top = offset_y + size_h;

		board.offset({ top: top, left: left });
		board.toggleClass('hdf-frate-expand');
		return;
	});
};

hdf.stickyQMenu = function () {
	var sticky = $('#hdf-quickmenu'),
		fixed = {'position': 'fixed', 'top':0, 'left':0, 'z-index': 1000},
		rel = {'position': 'relative'},
		sticky_ofs = sticky.offset().top;
    
    var stickyfn = function(){
        var scroll_top = $(window).scrollTop(); 
         
        if (scroll_top > sticky_ofs) { 
            sticky.css(fixed);
        } else {
            sticky.css(rel); 
        }   
    };
     
    stickyfn();
     
    $(window).scroll(function() {
		
		if($("#sidebar").css('position') == 'absolute'){
			var _h = $(".row-offcanvas").height() - $("#sidebar").height() +5;
			$('#sidebar').css("top", _h);
		}
         stickyfn();
    });
};

hdf.stickySidebar = function() {
	var quickmenu = $('#hdf-quickmenu'),
		sidebar = $('#sidebar'),
		hightQM = quickmenu.height();

		sidebar.stick_in_parent({offset_top: hightQM + 20});
};

hdf.scrollTop = function() {
	var btnfire = $('#hdf-movetop');

	btnfire.on('click', function(e) {
		e.preventDefault();
		$("html, body").animate({
            scrollTop: 0
        }, 700);
        return;
	});
};

$(function() {
	hdf.init();
});